package main

import (
	"fmt"
	"myapp/dal/model"
	"myapp/dal/query"

	"github.com/spf13/cobra"
)

var (
	// 接收端口号
	//port string

	serverCmd = &cobra.Command{
		Use:   "server",
		Short: "启动http服务,使用方法: app server --port=?",
		Run: func(cmd *cobra.Command, args []string) {
			// if port == "" {
			// 	log.Fatalf("port参数不能为空")
			// }
			goStart()
		},
	}

	name string

	roleCmd = &cobra.Command{
		Use:   "role",
		Short: "角色操作",
	}

	// 添加用户子命令
	addRoleCmd = &cobra.Command{
		Use:   "add",
		Short: "添加角色: role add --name=?",
		Run: func(cmd *cobra.Command, args []string) {
			query.SetDefault(db)

			role := model.Role{
				Name: name,
			}

			query.Role.Create(&role)
			fmt.Println("添加角色: ", name)
		},
	}

	// 删除用户子命令
	delRoleCmd = &cobra.Command{
		Use:   "del",
		Short: "删除角色: role del --name=?",
		Run: func(cmd *cobra.Command, args []string) {
			query.SetDefault(db)
			rs, err := query.Role.Where(query.Role.Name.Eq(name)).Delete()
			fmt.Println("删除角色: ", name, rs, err)
		},
	}
)

func init() {
	// 将server命令添加为rootCmd的子命令
	rootCmd.AddCommand(serverCmd)

	rootCmd.AddCommand(roleCmd)
	roleCmd.AddCommand(addRoleCmd)
	roleCmd.AddCommand(delRoleCmd)

	// server子命令接收port选项参数
	//serverCmd.Flags().StringVar(&port, "port", "", "端口号")
	roleCmd.PersistentFlags().StringVarP(&name, "name", "n", "", "角色名")
}
